﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using HISModel;
using Newtonsoft.Json;

namespace HISDAL
{
  public   class ghDAL
    {

        //链接数据库字符串
        SqlConnection con = new SqlConnection("Data Source=169.254.82.63;Initial Catalog=HospitalSystem;User ID=sa;password=asd_123");
        //获取医院下拉列表的数据
        public List<T_Hospital> GetDataHospital()
        {
            //打开数据库
            con.Open();
            string sql = "select * from T_Hospital";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            var s = JsonConvert.SerializeObject(dt);
            var data = JsonConvert.DeserializeObject<List<T_Hospital>>(s);
            con.Close();
            return data;
            
        }
        //获取科室下拉列表的数据
        public List<T_Office> GetDataOffice()
        {
            //打开数据库
            con.Open();
            string sql = "select * from T_Office";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            var o = JsonConvert.SerializeObject(dt);
            var data = JsonConvert.DeserializeObject<List<T_Office>>(o);
            con.Close();
            return data;
        }
        //获取医生级别下拉列表的数据
        public List<T_DoctorType> GetDatajibie()
        {
            //打开数据库
            con.Open();
            string sql = "select * from T_DoctorType";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            var o = JsonConvert.SerializeObject(dt);
            var data = JsonConvert.DeserializeObject<List<T_DoctorType>>(o);
            con.Close();
            return data;
        }
        //获取医生下拉列表的数据
        public List<T_Doctor> GetDataDoctor()
        {
            //打开数据
            con.Open();
            string sql = "select * from T_Doctor";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);

            var g = JsonConvert.SerializeObject(dt);
            var doctor = JsonConvert.DeserializeObject<List<T_Doctor>>(g);
            con.Close();
            return doctor;
        }
        //添加挂号信息
        public int GetDataGHinfo(Gh_Table m)
        {
            //打开数据
            con.Open();

            string sql = "insert into Gh_Table values('" + m.Gh_Reason + "','" + m.U_Name + "','" + m.Hosp_ID + "','" + m.Office_ID + "','" + m.Doctor_ID + "','1','" + m.Gh_Time + "','"+m.Gh_DoctorTypeID + "')";
||||||| .r28
            string sql = "insert into Gh_Table values('" + m.Gh_Reason + "','" + m.U_Name + "','" + m.Hosp_ID + "','" + m.Office_ID + "','" + m.Doctor_ID + "','1','" + m.Gh_Time + "','"+m.Doctor_TypeID+"')";
=======
<<<<<<< .mine
            string sql = "insert into Gh_Table values('" + m.Gh_Reason + "','" + m.U_Name + "','" + m.Hosp_ID + "','" + m.Office_ID + "','" + m.Doctor_ID + "','1','" + m.Gh_Time + "','" + m.Gh_DoctorTypeID + "')";
<<<<<<< .mine
||||||| .r31
||||||| .r24
            string sql = "insert into Gh_Table values('" + m.Gh_Reason + "','" + m.U_Name + "','" + m.Hosp_ID + "','" + m.Office_ID + "','" + m.Doctor_ID + "','1','" + m.Gh_Time + "')";
=======
            string sql = "insert into Gh_Table values('" + m.Gh_Reason + "','" + m.U_Name + "','" + m.Hosp_ID + "','" + m.Office_ID + "','" + m.Doctor_ID + "','1','" + m.Gh_Time + "','"+m.Doctor_TypeID+"')";
>>>>>>> .r30
=======
||||||| .r24
            string sql = "insert into Gh_Table values('" + m.Gh_Reason + "','" + m.U_Name + "','" + m.Hosp_ID + "','" + m.Office_ID + "','" + m.Doctor_ID + "','1','" + m.Gh_Time + "')";
=======
            string sql = "insert into Gh_Table values('" + m.Gh_Reason + "','" + m.U_Name + "','" + m.Hosp_ID + "','" + m.Office_ID + "','" + m.Doctor_ID + "','1','" + m.Gh_Time + "','"+m.Doctor_TypeID+"')";
>>>>>>> .r31
>>>>>>> .r30
>>>>>>> .r32
            SqlCommand cmd = new SqlCommand(sql, con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            con.Dispose();
            return result;

        }     //获取挂号类型表
      public List<T_DoctorType> GetDataGHtype()
        {
            //打开数据库
            con.Open();
            string sql = "select * from T_DoctorType";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            var t = JsonConvert.SerializeObject(dt);
            var type = JsonConvert.DeserializeObject<List<T_DoctorType>>(t);
            con.Close();
            return type;
        }
      //获取挂号记录表数据
      public List<GH_Record> GetDataGHrecord()
        {
            //打开数据库
            string sql = "select * from Gh_Table inner join T_Hospital on Gh_Table.Hosp_ID=T_Hospital.Hosp_ID inner join T_Doctor on Gh_Table.Doctor_ID= T_Doctor.Doctor_ID inner join T_Office on Gh_Table.Office_ID= T_Office.Office_ID inner join T_DoctorType on Gh_Table.Gh_DoctorTypeID=T_DoctorType.Doctor_TypeId";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            var r = JsonConvert.SerializeObject(dt);
            var info = JsonConvert.DeserializeObject<List<GH_Record>>(r);
            return info;
        }

    }
}
